package dao.impl;

import dao.CommentDao;
import domian.Comment;
import utils.BaseDAO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class CommentDaoImpl implements CommentDao {
    //添加评论
    @Override
    public Integer addComment(Comment comment) {
        String sql = "insert into comment(uid,fid,content,time,starLevel) values(?,?,?,?,?)";
        Object com[] = {comment.getUid(), comment.getFid(), comment.getContent(), comment.getTime(), comment.getStarLevel()};
        return BaseDAO.executeUpdate(sql, com);
    }

    //根据用户id和文件id查询评论信息
    @Override
    public Comment inquireComment(Integer uid, Integer fid) {
        Comment comment = null;
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from comment where uid=? and fid=?";
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setInt(2, fid);
            //执行SQL语句
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                String content = resultSet.getString("content");
                String time = resultSet.getString("time");
                Integer starLevel = resultSet.getInt("starLevel");
                comment = new Comment(id, uid, fid, content, time, starLevel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, resultSet);
        }
        return comment;
    }

    //根据fid查询评论+分页
    @Override
    public List<Comment> pagingComment(Integer fid, Integer which, Integer page) {
        List<Comment> comments = new ArrayList<>();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from comment left join user on comment.uid=user.uid where fid=?";
            if (which != null && page != null) {
                sql += " limit ?,?";
            }
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            ps.setInt(1, fid);
            if (which != null && page != null) {
                ps.setInt(2, which);
                ps.setInt(3, page);
            }
            //执行SQL语句
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                Integer uid = resultSet.getInt("comment.uid");
                String headerimg = resultSet.getString("headerimg");
                String nickname = resultSet.getString("nickname");
                String tel = resultSet.getString("tel");
                String content = resultSet.getString("content");
                String time = resultSet.getString("time");
                Integer starLevel = resultSet.getInt("starLevel");
                comments.add(new Comment(id, uid, fid, headerimg, nickname, tel, content, time, starLevel));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, resultSet);
        }
        return comments;
    }
}
